package com.woniu.controller;


import com.woniu.been.entity.*;
import com.woniu.service.CourseChaptersService;
import com.woniu.tools.CourseTreeBuilder;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chapter")
@CrossOrigin
public class CourseChaptersController {

    @Autowired
    CourseChaptersService courseChaptersService;


    // 获取目录 分类 阶段  课程
    @PostMapping("/getChapter") // chapter/getChapter
//    public ResponseResult<List<Courses>> getCapter(CourseCategories coursecategories){
    public ResponseResult<List<CategoryNode>> getCapter(CourseCategories coursecategories){
        List<CategoryNode> coursesz = courseChaptersService.getCapter(coursecategories);
//        List<Courses> coursesz = courseChaptersService.getCapter(coursecategories);
        if(coursesz!=null){
            return ResponseResult.success(coursesz);
        }else{
            return ResponseResult.fail(500,"服务端错误");
        }
    }

//
    // 根据id 获取所有的获取目录 分类 阶段  课程
    @PostMapping("/getChapterById") // chapter/getChapter
    public ResponseResult<List<Courses>> getChapterById(@RequestBody Courses course){
        List<Courses> coursesz = courseChaptersService.getChapterById(course);
        if(coursesz!=null){
            return ResponseResult.success(coursesz);
        }else{
            return ResponseResult.fail(500,"服务端错误");
        }
    }

    // List<VideoVO> getVideos( VideoVO video)
    @PostMapping("/getVideos") // chapter/getVideos
    public  ResponseResult<List<Video>>  getVideos(@RequestBody Video video){
        List<Video> coursesz = courseChaptersService.getVideos(video);
        if(coursesz!=null){
            return ResponseResult.success(coursesz);
        }else{
            return ResponseResult.fail(500,"服务端错误");
        }
    }
    // 查询某课程下所有章节
    @GetMapping
    public Result<List<CourseChapters>> list(@RequestParam Integer courseId) {
        return Result.success(courseChaptersService.getChaptersByCourseId(courseId));
    }

    // 新增章节
    @PostMapping
    public Result<?> add(@RequestBody CourseChapters chapter, @RequestParam Integer courseId) {
        courseChaptersService.addChapter(chapter, courseId);
        return Result.success("新增成功");
    }

    // 修改章节
    @PutMapping("/{id}")
    public Result<?> update(@PathVariable Integer id, @RequestBody CourseChapters chapter) {
        chapter.setChapterId(id);
        courseChaptersService.updateChapter(chapter);
        return Result.success("修改成功");
    }

    // 删除章节
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        courseChaptersService.deleteChapter(id);
        return Result.success("删除成功");
    }
}
